package com.wandian.weather.mvvm.fifteendayweather;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mylib.libcore.AppContext;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.mvvm.BaseMVVMActivity;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.TimeUtils;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.weather.R;
import com.wandian.weather.adapter.WeatherFifteenDayRecycleAdapter;
import com.wandian.weather.bean.AqiBean;
import com.wandian.weather.bean.WeatherInformationBean;
import com.wandian.weather.databinding.WeatherActivityFifteenDayWatherBinding;
import com.wandian.weather.tools.WeatherUtil;

import static com.mylib.libcore.config.AppSPKeyConfig.CITY_NAME;
import static com.mylib.libcore.config.AppSPKeyConfig.CITY_STREET_NAME;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description 15天天气
 **/
@Route(path = RouterConfig.ROUTER_ACTIVITY_WEATHER_FIFTEEN_DAY_WEATHER)
public class FifteenDayWeatherActivity extends BaseMVVMActivity<FifteenDayWeatherViewModel> {
    private WeatherActivityFifteenDayWatherBinding dataBinding;
    private WeatherFifteenDayRecycleAdapter weatherFifteenDayRecycleAdapter;
    private WeatherInformationBean weatherInformationBean;
    private String mCTime;
    @Override
    public void init(Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.weather_activity_fifteen_day_wather);
        dataBinding.setLifecycleOwner(this);
        dataBinding.setViewModel(mViewModel);
        //更具时间更换背景
        TimeUtils.customHomeBgByTime(this,dataBinding.llDays);
        String weatherJson = AppContext.getInstance().getAppPreferencesHelper().getKeyValue("WeatherInformationBean");
        if(!UIUtils.isEmpty(weatherJson)){
            weatherInformationBean = (WeatherInformationBean) JsonUtil.parseJsonToBean(weatherJson,WeatherInformationBean.class);
        }
        initClink();
        initRecycle();
        initViewData();
    }

    private void initViewData(){
        String city_name = AppContext.getInstance().getAppPreferencesHelper().getKeyValue(CITY_NAME);
        String city_street_name = AppContext.getInstance().getAppPreferencesHelper().getKeyValue(CITY_STREET_NAME);
        if(!UIUtils.isEmpty(city_name)){
            dataBinding.bindSysToolBar.toolBarTvTitle.setText(city_name);
        }else {
            dataBinding.bindSysToolBar.toolBarTvTitle.setText("所在城市");
        }
        if(!UIUtils.isEmpty(city_street_name)) dataBinding.tvLocalAddress.setText(city_street_name);

    }

    private void initRecycle(){

        weatherFifteenDayRecycleAdapter = new WeatherFifteenDayRecycleAdapter(R.layout.weather_item_15days_top);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        dataBinding.recycler.setLayoutManager(linearLayoutManager);
        dataBinding.recycler.setAdapter(weatherFifteenDayRecycleAdapter);
        if(!UIUtils.isEmpty(weatherInformationBean)){
            weatherFifteenDayRecycleAdapter.setNewData(weatherInformationBean.getWeather30());
            notifyItemWeatherView(weatherInformationBean.getWeather30().get(0));
        }
        weatherFifteenDayRecycleAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                WeatherInformationBean.Weather30Bean bean = (WeatherInformationBean.Weather30Bean)baseQuickAdapter.getData().get(i);
                if(!UIUtils.isEmpty(bean)){
                    notifyItemWeatherView(bean);
                }
                weatherFifteenDayRecycleAdapter.setSelectPos(i);
                weatherFifteenDayRecycleAdapter.notifyDataSetChanged();
            }
        });
    }

    private void notifyItemWeatherView(WeatherInformationBean.Weather30Bean item) {
        try {
            mCTime = item.getNl().getDate();

            String sunriseSunset = item.getSunup()+"/"+item.getSundown();
            dataBinding.tvNew15daysSunriseSunset.setText(sunriseSunset);

            String temperature = String.format("%s~%s°", item.getLtemp(), item.getHtemp());
            dataBinding.tvNew15daysTemperature.setText(temperature);
            dataBinding.tvNew15daysTemperatureTips.setText(item.getWeather());


            AqiBean aqiBean = WeatherUtil.getAqiBean(this, Integer.parseInt(item.getAqi()));
            String aqi = String.format("%s %s", item.getAqi(), aqiBean.getAqiName());

            dataBinding.tvNew15daysAqi.setText(aqi);
            String wind = String.format("%s %s", item.getWD(), item.getWS());
            dataBinding.tvNew15daysWind.setText(wind);

            try {
                //湿度
                int humidity_min = (int) (Float.parseFloat(item.getHumidity().getMin())*100);
                int humidity_max = (int) (Float.parseFloat(item.getHumidity().getMax())*100);
                dataBinding.tvNew15daysHumidity.setText(humidity_min+"%~"+humidity_max+"%");
            } catch (Exception e) {
                e.printStackTrace();
            }

            //紫外线

            dataBinding.tvNew15daysUltravioletRay.setText(item.getUltraviolet().getDesc()+" "+item.getUltraviolet().getIndex()+"级");
            float pha = Float.parseFloat(item.getPres())/100;
            int pha1 = (int) pha;
            dataBinding.tvNew15daysAirPressure.setText(pha1+" hPa");
            int resId = WeatherUtil.getWeatherStateImage(String.valueOf(item.getImg_num()), false);
            dataBinding.imgWeatherImg.setImageResource(resId);
            String date = weatherInformationBean.getNl().getDate();
            if(TextUtils.equals(date, mCTime)){
                String currentTemp =  getResources().getString(R.string.current_temp, weatherInformationBean.getBase().getWeather(),
                        weatherInformationBean.getBase().getStemp());
                dataBinding.tvCurrentTemp.setText(currentTemp);
                String tiganwendu = getResources().getString(R.string.tiganwendu, weatherInformationBean.getBase().getFeeltemp());
                dataBinding.tvTiganWendu.setText(tiganwendu);
            }else {

                String DayTemp = getResources().getString(R.string.day_temp, item.getTemp().getMin(), item.getTemp().getMax());
                String nightTemp = getResources().getString(R.string.night_temp, item.getTemp().getNmin(), item.getTemp().getNmax());
                dataBinding.tvCurrentTemp.setText(DayTemp);
                dataBinding.tvTiganWendu.setText(nightTemp);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void initClink(){
        dataBinding.bindSysToolBar.toolBarIvBack.setOnClickListener(v -> {
            finish();
        });

    }



}
